package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.ExamPoints;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.ExamPointsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class ExamPointsService {

    @Autowired
    private ExamPointsMapper examPointsMapper;

    /**
     * 新增
     * @param examPoints
     * @return
     */
    public int insert(ExamPoints examPoints){
        examPoints.setStatus(BaseStatusEnum.INVALID.getValue());
        examPointsMapper.insert(examPoints);
        return examPoints.getId();
    }

    /**
     * 更新
     * @param examPoints
     * @return
     */
    public int update(ExamPoints examPoints){
        if(examPoints.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return examPointsMapper.updateByPrimaryKeySelective(examPoints);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return examPointsMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ExamPoints selectById(Integer id){
        return examPointsMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据参数查询列表
     * @param name 名称
     * @param status 状态
     * @return
     */
    public List<ExamPoints> selectByParams(String name, Integer status){
        return examPointsMapper.selectByParams(name, status);
    }

    /**
     * 根据参数查询列表
     * @param province 省
     * @param city 市
     * @return
     */
    public List<ExamPoints> selectByProvinceAndCity(String province, String city){
        return examPointsMapper.selectByProvinceAndCity(province, city);
    }
    /**
     * 根据参数查询列表
     * @return
     */
    public List<ExamPoints> selectAll(String province, String city){
        return examPointsMapper.selectGroup(province, city);
    }

    /**
     * 查询当前存在的考核点包含的地区信息
     * @return
     */
    public List<Map<String,Object>> findAreaList(){
        List<ExamPoints> examPoints = examPointsMapper.selectByParams(null, BaseStatusEnum.VALID.getValue());
        List<Map<String,Object>> areas = new ArrayList<>();
        List<String> provinces = examPoints.stream().map(e-> e.getProvince()).distinct().collect(Collectors.toList());
        for(String province : provinces){
            Map<String,Object> map = new HashMap<>();
            map.put("label",province);
            List<Map<String,Object>> children = new ArrayList<>();
            List<String> cities = examPoints.stream().filter(e->e.getProvince().equals(province)).map(e->e.getCity()).distinct().collect(Collectors.toList());
            for(String city : cities){
                Map<String,Object> child = new HashMap<>();
                child.put("label",city);
                children.add(child);
            }
            map.put("children",children);
            areas.add(map);
        }
        return areas;
    }
}
